import os

import time

from FastFitsFileTools.FitsTools import Fitscut, Fits
from FastFitsFileTools.FitsTools.FitsCut import exeCutFreq
from FastFitsFileTools.FitsTools.FitsMerge import FitsMerge
from FastFitsFileTools.FitsTools.FitsOutput import wirtreToFileForFast


def usage():
    print """

"-f", "file:      [please give ***path/filename***]"

"-b", "beginfreq: [start freq ]"
"-e", "endfreq:   [end  freq]"

"-t", "strattime: [start time]"
"-v", "endtime:   [end time]"
"-d", "dec(J2000):[dd:mm:ss]"
"-o", "output:    [output path]"

"""
def readPath(filename):
    while 1:
        files = filename.split('/')[len(filename.split('/')) - 1]

        filename2 = files[:len(files) - 9]+ str('%04d' % (int(files[len(files) - 9:len(files) - 5]) + 1))+'.fits'
        #print filename2
        filename2_path='/'
        for i in [x + 1 for x in range(filename.split('/').__len__() - 2)]:
            filename2_path+=(filename.split('/')[i]+'/')

        filename2=filename2_path+filename2
        print filename2
        if os.path.exists(filename2) and os.path.getsize(filename2)>2000000000:
            return filename2
            break;
        else:
            time.sleep(10)
            print 'waiting for new fits'
            #readPath(filename)




if __name__ == '__main__':
    import getopt, sys

    try:
        opts, args = getopt.getopt(sys.argv[1:], "h:m:f:b:e:o:z:c:t:v:i:s:l:x:y:d:",
                                   ["help", "mode=", "file=", "beginfreq=",
                                    "endfreq=", "output=", "outname", "strattime=", "endtime", "interval=",
                                    "freqspace=","subintoffset=","Fa","Fb","dec="])

    except getopt.GetoptError:
        # print help information and exit:
        usage()
        sys.exit(2)
    if len(sys.argv) == 1:
        usage()
        sys.exit(2)
    for o, a in opts:
        if o in ("-h", "--help"):
            usage()
            sys.exit()
        if o in ("-m", "--mode"):
            mode = str(a)
        if o in ("-f", "--file"):
            filename = str(a)
        if o in ("-b", "--beginfreq"):
            startfreq = float(a)
        if o in ("-e", "--endfreq"):
            endfreq = float(a)
        if o in ("-o", "--output"):
            outpath = str(a)
        if o in ("-z", "--outname"):
            outname = str(a)
            # print outname
        if o in ("-c", "--cutid"):
            cutid = int(a)
        if o in ("-t", "--strattime"):
            starttime = float(a)
            # print starttime
        if o in ("-v", "--endtime"):
            endtime = float(a)
        if o in ("-i", "--interval"):
            interval = float(a)
        if o in ("-s", "--freqspace"):
            freqSpace = int(a)

        if o in ("-l", "--subintoffset"):
            subintoffset = int(a)
        if o in ("-d", "--dec"):
            dec = str(a)
        if o in ("-x", "--Fa"):
                aa = float(a)
        if o in ("-y", "--Fb"):
                bb = float(a)
        while 1:
            fits1 = Fits(filename)
            filename2=readPath();
            fits2 = Fits(filename2)
            # fits.centerFreq = (startfreq + endfreq) / 2

            fmerge = FitsMerge(fits1, fits2, 0, 0, 0)
            # self.mergePolae(0)
            fits_merged = fmerge.merge()
            exeCutFreq(fits_merged, startfreq, endfreq, starttime, endtime,dec,str(outpath+str(i/12)))



